package cn.jiyun.controller;
import cn.jiyun.pojo.Constactor;
import cn.jiyun.pojo.Developer;
import cn.jiyun.pojo.Project;
import cn.jiyun.result.PageResult;
import cn.jiyun.result.QueryPageBean;
import cn.jiyun.result.Result;
import cn.jiyun.service.DeveloperService;
import cn.jiyun.service.ProjectService;
import cn.jiyun.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("developer")
public class DeveloperController {

    @Autowired
    private DeveloperService developerService;

    @Autowired
    private ProjectService projectService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        List<Map> list=developerService.findPage(queryPageBean);
        /*分页查询*/
        PageResult result = new PageResult();
        result.setRows(list);
        /*查询记录数*/
        long i=developerService.jilu();
        result.setTotal(i);
        return result;
    }

    @RequestMapping("add")
    public String add(String dname,int[] ids){
        Developer developer = new Developer();
        developer.setDid(UUIDUtil.getUuid());
        developer.setDname(dname);
        developer.setUpdatetime(new Date());
        developerService.add(developer);

        long i=projectService.flag(developer,ids);
        return "2";
    }

   /* @RequestMapping("init")
    public List<developer> init(){
        return developerService.init();
    }
*/
   /* @RequestMapping("init2")
    public List<Constactor> init2(){
        return developerService.init2();
    }*/

    @RequestMapping("edit")
    public Result edit(@RequestBody Developer developer){
        int i=developerService.edit(developer);
        if (i>0){
            return new Result(true,"success");
        }else {
            return new Result(false,"error");
        }
    }

    @RequestMapping("delete")
    public Result delete(String did){
        try {
            developerService.delete(did);
            return new Result(true,"success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"error");
        }
    }

    @RequestMapping("findById")
    public List<Developer> findById(String did){
        return developerService.findById(did);
    }

}
